﻿Imports System.Windows.Forms

Public Class FormGridHost
    Private Const Checkall = "Check All"
    Private Const UncheckAll = "Un Check All"
    Private _sourceTbl As DataTable
    Private _SourceDs As DataSet
    Public Property SourceDataset As DataSet
        Get
            If _SourceDs Is Nothing Then
                _SourceDs = New DataSet
            End If
            Return _SourceDs
        End Get
        Set(value As DataSet)
            _SourceDs = value
            _sourceTbl = _SourceDs.Tables(0)
        End Set
    End Property

    Private Sub FormHost_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        For Each cl As DataColumn In _sourceTbl.Columns
            If cl.DataType = GetType(String) Then
                cmbColumns.Items.Add(cl.ColumnName)
            End If
            Dim Mnu As ToolStripMenuItem = GridviewContextMenu.Items.Add(cl.ColumnName)
            Mnu.Name = cl.ColumnName
            Mnu.CheckOnClick = True
            Mnu.Checked = True
            Mnu.Tag = cl.Ordinal
            chkLstColumnBox.Items.Add(cl.ColumnName, True)
            AddHandler Mnu.CheckedChanged, AddressOf ChangeColumnVisible
        Next
        AddHandler chkLstColumnBox.ItemCheck, AddressOf chkLstColumnBox_ItemCheck
        DgListView.DataSource = _sourceTbl.DefaultView
        btnCheckAll.Text = UncheckAll
    End Sub

    Private Sub TxtFilter_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtFilter.TextChanged
        If cmbColumns.SelectedIndex > -1 AndAlso TxtFilter.Text.Trim.Length > 0 Then
            _sourceTbl.DefaultView.RowFilter = cmbColumns.Text + " like '%" + TxtFilter.Text + "%'"
        Else
            _sourceTbl.DefaultView.RowFilter = ""
        End If
        DgListView.DataSource = _sourceTbl.DefaultView
    End Sub

    Private Sub ChangeColumnVisible(sender As Object, e As EventArgs)
        Dim Ts As ToolStripMenuItem = sender
        DgListView.Columns(Ts.Name).Visible = Ts.Checked
        RemoveHandler chkLstColumnBox.ItemCheck, AddressOf chkLstColumnBox_ItemCheck
        chkLstColumnBox.SetItemChecked(Ts.Tag, Ts.Checked)
        AddHandler chkLstColumnBox.ItemCheck, AddressOf chkLstColumnBox_ItemCheck
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        For Each mnu As ToolStripMenuItem In GridviewContextMenu.Items
            If mnu IsNot sender Then
                mnu.Checked = True
            End If
        Next
    End Sub

    Private Sub chkLstColumnBox_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs)
        GridviewContextMenu.Items(e.Index + 1).PerformClick()
    End Sub

    Private Sub btnCheckAll_Click(sender As System.Object, e As System.EventArgs) Handles btnCheckAll.Click
        If btnCheckAll.Text = UncheckAll Then
            For Each item As Integer In chkLstColumnBox.CheckedIndices
                chkLstColumnBox.SetItemChecked(item, False)
            Next
            btnCheckAll.Text = Checkall
        Else
            For item As Integer = 0 To chkLstColumnBox.Items.Count - 1
                chkLstColumnBox.SetItemChecked(item, True)
            Next
            btnCheckAll.Text = UncheckAll
        End If
    End Sub
End Class